package com.surprise.bweb;

import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.interceptor.CacheErrorHandler;
import org.springframework.cache.interceptor.CacheResolver;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleCacheErrorHandler;
import org.springframework.cache.interceptor.SimpleCacheResolver;
import org.springframework.cache.interceptor.SimpleKeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.yaml.snakeyaml.Yaml;

import com.surprise.biz.BizBeanConfig;
import com.surprise.webcommon.WebcommonBeanConfig;

import net.sf.ehcache.config.CacheConfiguration;

/**
 * BwebBeanConfig.java
 * @author nick
 * @since 2015年7月19日
 */
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.surprise.bweb",
        excludeFilters = @Filter(type = FilterType.REGEX, pattern = {
                "com.surprise.bweb.util", "com.surprise.bweb.po",
                "com.surprise.bweb.bo", "com.surprise.bweb.controller"}))
@Import({WebcommonBeanConfig.class, BizBeanConfig.class})
@EnableCaching
public class BwebBeanConfig implements CachingConfigurer {
	@Bean
	public CommonsMultipartResolver multipartResolver() {
		CommonsMultipartResolver ret = new CommonsMultipartResolver();
		ret.setDefaultEncoding("UTF-8");
		ret.setMaxUploadSize(1048576L); // 1MB
		return ret;
	}
	
	@Bean(destroyMethod="shutdown")
	public net.sf.ehcache.CacheManager ehCacheManager() {
			CacheConfiguration cacheConfiguration = new CacheConfiguration();
	        cacheConfiguration.setName("myCacheName");
	        cacheConfiguration.setMemoryStoreEvictionPolicy("LRU");
	        cacheConfiguration.setMaxEntriesLocalHeap(1000);
	        // cache缓存时间30分钟
	        cacheConfiguration.setTimeToLiveSeconds(1800);

	        net.sf.ehcache.config.Configuration config = new net.sf.ehcache.config.Configuration();
	        config.addCache(cacheConfiguration);

	        return net.sf.ehcache.CacheManager.newInstance(config);	
	}
	
	@Bean
    public AuthIgnoreConfig authIgnores() {
        Yaml yaml = new Yaml();
        AuthIgnoreConfig conf = yaml.loadAs(
                BwebBeanConfig.class.getClassLoader().getResourceAsStream("auth_ignore_urls.yaml"),
                AuthIgnoreConfig.class);
        return conf;
    }

	@Override
	public CacheManager cacheManager() {
		return new EhCacheCacheManager(ehCacheManager());
	}

	@Override
	public CacheResolver cacheResolver() {
		return new SimpleCacheResolver();
		
	}

	@Override
	public KeyGenerator keyGenerator() {
		return new SimpleKeyGenerator();
	}

	@Override
	public CacheErrorHandler errorHandler() {
		return new SimpleCacheErrorHandler();
	}
}
